package com.dodi.spring.conversionService;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;

public class String2DateConverter implements ConversionService {

	@Override
	public boolean canConvert(Class<?> sourceType, Class<?> targetType) {

		if(Date.class.isAssignableFrom(targetType)){
			return true;
		}
		System.out.println(targetType);
		return false;
	}

	@Override
	public boolean canConvert(TypeDescriptor sourceType,
			TypeDescriptor targetType) {

		if(Date.class.isAssignableFrom(targetType.getObjectType())){
			return true;
		}
		return false;
	}

	@Override
	public <T> T convert(Object source, Class<T> targetType) {
		return null;
	}

	@Override
	public Object convert(Object source, TypeDescriptor sourceType,
			TypeDescriptor targetType) {

		if(String.class.isAssignableFrom(sourceType.getObjectType())){
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			try {
				return format.parse((String) source);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		return null;
	}



}
